from . import miniss_base
class  ss_move_gap(miniss_base):
    def __init__(self,config,pos,bscmd,gap=3,activeprice=None,triggertimes=1):
        super().__init__(config, pos, bscmd, activeprice, triggertimes)
        self.gip=gap
        self.price=None
        self.times=0
        """
        1.移动通道突破；可以用于移动止损，也可以用于跟踪反向下单
        2.gip:通道宽度
        """
    def ontick(self,d):
        self.deal_on(d)
    def deal_buy(self,d):
        tick=d["tick"][-1]
        t=tick["LastPrice"]
        if self.price:
            if t>self.price:
                self.price=t
            elif self.price>=t+self.gip:
                self.times=self.times+1
                if self.times==self.triggertimes:
                    self.cmdorder[self.bscmd](t,tick["timekey"])
                    self.stop()
        else:
            self.price=t
    def deal_sell(self,d):
        tick = d["tick"][-1]
        t = tick["LastPrice"]
        if self.price:
            if t < self.price:
                self.price = t
            elif self.price <= t - self.gip:
                self.times = self.times + 1
                if self.times == self.triggertimes:
                    self.cmdorder[self.bscmd](t, tick["timekey"])
                    self.stop()
        else:
            self.price = t
class  ss_to_price(miniss_base):
    def __init__(self,config,pos,bscmd,toprice,activeprice=None,triggertimes=1):
        super().__init__(config, pos, bscmd, activeprice, triggertimes)
        self.toprice=toprice
        self.times=0
    def ontick(self,d):
        self.deal_on(d)
    def deal_buy(self,d):
        tick=d["tick"][-1]
        t=tick["LastPrice"]
        if self.toprice<=t:
            self.times=self.times+1
            if self.times==self.triggertimes:
                self.cmdorder[self.bscmd](t,tick["timekey"])
                self.stop()
    def deal_sell(self,d):
        tick = d["tick"][-1]
        t = tick["LastPrice"]
        if self.toprice >= t:
            self.times = self.times + 1
            if self.times == self.triggertimes:
                self.cmdorder[self.bscmd](t, tick["timekey"])
                self.stop()
class  ss_to_gap(miniss_base):
    def __init__(self,config,pos,bscmd,openprice,gap,activeprice=None,triggertimes=1):
        super().__init__(config, pos, bscmd, activeprice, triggertimes)
        if bscmd in ["buy","buyopen","buyclose"]:
            self.toprice = self.openprice - gap
        else:
            self.toprice = self.openprice + gap
        self.openprice=openprice
        self.gip=gap
        self.times=0
    def ontick(self,d):
        self.deal_on(d)
    def deal_buy(self,d):
        tick=d["tick"][-1]
        t=tick["LastPrice"]
        if self.toprice<=t:
            self.times=self.times+1
            if self.times==self.triggertimes:
                self.cmdorder[self.bscmd](t,tick["timekey"])
                self.stop()
    def deal_sell(self,d):
        tick = d["tick"][-1]
        t = tick["LastPrice"]
        if self.toprice >= t:
            self.times = self.times + 1
            if self.times == self.triggertimes:
                self.cmdorder[self.bscmd](t, tick["timekey"])
                self.stop()
